class TrustedfileController < ApplicationController
  
  def make
    require 'set'
  

	ids=params[:identifier]
	ver=params[:ver].to_i
	
	port_ips=params[:port].split(':',2)
	cm=ConnectionManager.new
	cm.refresh_mynet(ids,request.remote_ip,port_ips[0],port_ips[1],nil)

	UserConnection.update_all(['routerip=:routerip,thetime=:thetime,port=:port,ips=:ips',{:routerip=>request.remote_ip,:thetime=>Time.new,:port=>port_ips[0],:ips=>port_ips[1]}],["friend_id=:friend_id and user_id in (select friends.friend_id from friends where friends.user_id=:friend_id)",{:friend_id=>ids}])
	#ActiveRecord::Base.connection().update "UPDATE sometable SET used=1 WHERE id=#{id}"; 
  	ucs2=UserConnection.find(:all,:conditions=>["user_connections.friend_id=:friend_id and friends.user_id=:friend_id",{:friend_id=>ids}],:joins=>"inner join friends on (friends.friend_id=user_connections.user_id)")
    string=''
    kerjos=Set.new
    uc=UserConnection.find(:all,:conditions=>["(user_connections.user_id=:user_id or user_connections.user_id in (select friends.friend_id from friends where friends.user_id=:user_id)) and thetime > :thetime ",{:user_id=>ids,:thetime=>6.minutes.ago}])
    uc.each {|u2|
    	unless kerjos.include?u2.friend_id
			kerjos<< u2.friend_id
			if (ver>=1)
				string << u2.friend_id.to_s+":"+u2.routerip+":"+u2.port.to_s+":"+u2.ips+"\n"
			else
				string << u2.friend_id.to_s+"\\:"+u2.routerip+"\\:"+u2.port.to_s+"\\:"+u2.ips.gsub(/:/,"\\:")+"\n"
			end
		end
	}
	
	if (ver>=1)
		hfs=HotfileClick.find(:all,:conditions=>["user_id=:user_id",{:user_id=>ids}])
		unless hfs.nil?
			manager=NetManager.new(ids)
			hfs.each{|f|
				manager.join_net(f.swarm_id)
				string << "__file__:"+f.filesizelong.to_s+":"+f.hashkey.to_s+":"+f.name.to_s+":"+f.description+"\n"
				f.destroy
			}
		end
    end
    
    @content=string.chomp
    @content_type='text/plain'
    response.headers["Content-Type"] = @content_type
    #logger.debug("request.remote_ip => "+request.remote_ip+"time=>"+Time.new.to_s+"vincent messege trustedpeers=>\n"+@content+"\n end vicent messege")
    render :layout=>false
  end
end
